<?php

namespace App\Http\Requests\Business;

use Illuminate\Foundation\Http\FormRequest;

class PayerRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'name' => 'required|max:100',
            'category_code' => 'exists:payer_category,code',
        ];
        switch (request()->method) {
            case 'POST':
                $rules['code'] = 'required|numeric|unique:payer,code';
                break;
            case 'PUT':
                $rules['code'] = 'required|numeric|unique:payer,code,' . request()->payer;
                break;
            default:
                break;
        }

        return $rules;
    }

    public function messages()
    {
        return [
            'code.required' => '请输入付款方id！',
            'code.numeric' => '付款方id为数字格式！',
            'code.unique' => '付款方id已经存在！',
            'name.required' => '请输入付款方名称！',
            'name.max' => '付款方名称长度最大为100个字符！',
            'category_code.exists' => '请选择客户分类！',
        ];
    }
}
